package handler

import (
	"encoding/json"
	"login-master/cache"
	"login-master/model"
	"time"

	"github.com/gin-gonic/gin"
)

func UserAddRole(c *gin.Context) {

	uid := c.Query("uid")
	rid := c.Query("rid")

	data := model.UserRole{
		Uid: uid,
		Rid: rid,
	}

	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func UserRole(c *gin.Context) {

	uid := c.Query("uid")

	//SELECT
	//*
	//	FROM
	//roles
	//LEFT JOIN user_roles ON user_roles.rid = roles.id
	//WHERE
	//user_roles.uid = ?,

	var roles []model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid = ?", uid).
		Debug().
		Find(&roles)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": roles,
	})

}

func List(c *gin.Context) {

	var user []model.User

	redis_list_sys := "redis_list"

	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_sys).Result()
	if result == "" {
		model.DB.Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_list_sys, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &user)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": user,
	})
	return

}

func List1(c *gin.Context) {

	var user []model.User

	redis_list_sys := "redis_list"
	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_sys).Result()
	if result == "" {
		model.DB.Create(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_list_sys, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &user)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": user,
	})
	return

}

func List2(c *gin.Context) {

	var user []model.User

	redis_list_sys := "redis_list"
	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_sys).Result()
	if result == "" {
		model.DB.Find(&user)
		jsonData, _ := json.Marshal(&user)
		cache.Rdb.Set(cache.Ctx, redis_list_sys, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &user)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": user,
	})
	return

}

func List3(c *gin.Context) {

	var user []model.User

	redis_list_sys := "redis_list"
	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_sys).Result()

	if result == "" {
		model.DB.Create(&user)
		jsonData, _ := json.Marshal(&user)
		cache.Rdb.Set(cache.Ctx, redis_list_sys, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &user)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": user,
	})
	return

}

func List4(c *gin.Context) {

	var user []model.User

	redis_list_sys := "redis_list"
	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_sys).Result()
	if result == "" {
		model.DB.Create(&user)
		jsonData, _ := json.Marshal(&user)
		cache.Rdb.Set(cache.Ctx, redis_list_sys, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &user)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": user,
	})
	return
}
